<?php
	session_start();
	if(file_exists('database.php')) {
		require_once 'database.php';
	}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
   <HEAD>
      <TITLE>Login</TITLE>
      <meta charset="utf8"/>
      <link rel="stylesheet" href="css/style.css">
   </HEAD>
   <BODY>
      <div class="main box">
         <div class="content box"> 
      		<form method="POST">
             <a href="login.php">Към Логина</a><br>
      			<div>Username:<input type="text" name="user"/></div>
      			<div>Password:<input type="password" name="pass"/></div>
      			<div>Money:<input type="text" name="money"/></div>
      			<input type="submit" value="Влез"/>
      		</form>
            <!-- <a href="logout.php">Излез от профила</a><br> -->
         </div>
      </div>

   		<?php
   			$flag = true;
   			if($_POST) {
   				$user = trim($_POST['user']);
   				$pass = trim($_POST['pass']);
   				$money = trim($_POST['money']);
   				$sql = "SELECT user, pass FROM user";
   				$result = mysqli_query($db, $sql);
   				while($row = $result->fetch_array()) {
   					if($row['user'] === $user && $row['pass'] === md5($pass)) {
   						$flag = false;	

   					}
   				}
   				if($flag) {
                  $pass = mysql_real_escape_string(md5($pass));
   					$sql = "INSERT INTO user(user, pass, money) VALUES('$user', '$pass', $money)";
   					mysqli_query($db, $sql);
   					echo "<p>Ти се регистрира успешно</p>";
   					mysqli_close($db);
   				}
   			}
   		?>
   </BODY>
</HTML>